package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album")
@SuppressWarnings({"all"})
public class BaseCategoryApiController {

	@Autowired
	private BaseCategoryService baseCategoryService;

	@Autowired
	private AlbumInfoService albumInfoService;


	/**
	 * /api/album/category/findAllCategory1
	 * 查询所有的一级分类信息
	 * @return
	 */
	@GetMapping("category/findAllCategory1")
	public Result<List<BaseCategory1>> findAllCategory1(){
		List<BaseCategory1> baseCategory1List = baseCategoryService.list();
		return Result.ok(baseCategory1List);
	}



	/**
	 * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
	 * 查询专辑声音分页列表
	 * @param page
	 * @param limit
	 * @param albumId
	 * @return
	 */
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	@GuiGuLogin(required = false)
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long page, @PathVariable Long limit, @PathVariable Long albumId){
		Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(page, limit);
		Long userId = AuthContextHolder.getUserId();
		albumTrackListVoPage = albumInfoService.findAlbumTrackPage(userId, albumTrackListVoPage, albumId);
		return Result.ok(albumTrackListVoPage);
	}



	/**
	 * /api/album/albumInfo/getAlbumStatVo/{albumId}
	 * 根据专辑ID获取专辑统计信息
	 * @param albumId
	 * @return
	 */
	@GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
	public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId) {
		AlbumStatVo albumStatVo = albumInfoService.getAlbumStatVo(albumId);
		return Result.ok(albumStatVo);
	}


	/**
	 * /api/album/category/getBaseCategoryList/{category1Id}
	 * 根据一级分类id获取全部分类信息
	 * @param category1Id
	 * @return
	 */
	@GetMapping("/category/getBaseCategoryList/{category1Id}")
	public Result<JSONObject> getBaseCategoryList(@PathVariable Long category1Id) {
		JSONObject baseCategoryList = baseCategoryService.getBaseCategoryList(category1Id);
		return Result.ok(baseCategoryList);
	}


	/**
	 * /api/album/category/findTopBaseCategory3/{category1Id}
	 * 根据一级分类Id查询三级分类列表
	 * @param category1Id
	 * @return
	 */
	@GetMapping("/category/findTopBaseCategory3/{category1Id}")
	public Result<List<BaseCategory3>> getTop7BaseCategory3(@PathVariable Long category1Id) {
		List<BaseCategory3> baseCategory3List = baseCategoryService.getTop7BaseCategory3(category1Id);
		return Result.ok(baseCategory3List);
	}




	/**
	 * /api/album/category/getCategoryView/{category3Id}
	 * 根据三级分类Id 获取到分类信息
	 * @param category3Id
	 * @return
	 */
	@GetMapping("/category/getCategoryView/{category3Id}")
	public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryService.getCategoryView(category3Id);
		return Result.ok(baseCategoryView);
	}


	/**
	 * /api/album/albumInfo/findUserAllAlbumList
	 * 获取当前用户全部专辑列表
	 * @param id
	 * @param albumInfoVo
	 * @returnH
	 */
	@GetMapping("albumInfo/findUserAllAlbumList")
	@GuiGuLogin
	public Result<List<AlbumInfo>> findUserAllAlbumList() {
		Long userId = AuthContextHolder.getUserId();
		List<AlbumInfo> albumInfoList = albumInfoService.findUserAllAlbumList(userId);
		return Result.ok(albumInfoList);
	}


	/**
	 * /api/album/albumInfo/updateAlbumInfo/{id}
	 * 修改专辑
	 * @param id
	 * @param albumInfoVo
	 * @return
	 */
	@PutMapping("/albumInfo/updateAlbumInfo/{id}")
	public Result updateAlbumInfo(@PathVariable Long id, @RequestBody AlbumInfoVo albumInfoVo){
		albumInfoService.updateAlbumInfo(id, albumInfoVo);
		return Result.ok();
	}

	/**
	 * /api/album/albumInfo/getAlbumInfo/{id}
	 * 根据ID查询专辑信息
	 * @param id
	 * @return
	 */
	@GetMapping("/albumInfo/getAlbumInfo/{id}")
	public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id){
		AlbumInfo albumInfo = albumInfoService.getAlbumInfo(id);
		return Result.ok(albumInfo);
	}


	/**
	 * /api/album/albumInfo/removeAlbumInfo/{id}
	 * 根据id删除专辑信息
	 * @param page
	 * @param limit
	 * @param albumInfoQuery
	 * @return
	 */
	@DeleteMapping("/albumInfo/removeAlbumInfo/{id}")
	public Result removeAlbumInfo(@PathVariable Long id){
		albumInfoService.removeAlbumInfo(id);
		return Result.ok();
	}



	/**
	 * /api/album/albumInfo/findUserAlbumPage/{page}/{limit}
	 * 查看当前用户专辑分页列表
	 * @param page
	 * @param limit
	 * @param albumInfoQuery
	 * @return
	 */
	@PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<AlbumListVo>> findUserAlbumPage(@PathVariable Long page, @PathVariable Long limit, @RequestBody AlbumInfoQuery albumInfoQuery){
		Page<AlbumListVo> pageInfo = new Page<>(page, limit);
		Long userId = AuthContextHolder.getUserId();
		albumInfoQuery.setUserId(userId);
		pageInfo = albumInfoService.findUserAlbumPage(pageInfo,albumInfoQuery);
		return Result.ok(pageInfo);
	}



	/**
	 * ///api/album/albumInfo/saveAlbumInfo
	 * 保存专辑信息
	 * @param albumInfoVo
	 * @return
	 */
	@Operation(summary = "保存专辑")
	@PostMapping("/albumInfo/saveAlbumInfo")
	@GuiGuLogin
	public Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo) {
		//TODO 保存专辑信息
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		albumInfoService.saveAlbumInfo(userId, albumInfoVo);
		return Result.ok();
	}

	/**
	 * 查询所有分类（1、2、3级分类）
	 *
	 * @return
	 */
	@Operation(summary = "查询所有分类（1、2、3级分类）")
	@GetMapping("/category/getBaseCategoryList")
	public Result<List<JSONObject>> getBaseCategoryList() {
		List<JSONObject> list = baseCategoryService.getBaseCategoryList();
		return Result.ok(list);
	}

	/**
	 * 根据一级分类Id获取分类（标签名包含标签值） 列表
	 * @param category1Id 1级分类ID
	 * @return
	 */
	@Operation(summary = "根据一级分类Id获取分类（标签名包含标签值） 列表")
	@GetMapping("/category/findAttribute/{category1Id}")
	public Result<List<BaseAttribute>> getAttributesByCategory1Id(@PathVariable Long category1Id){
		List<BaseAttribute> list = baseCategoryService.getAttributesByCategory1Id(category1Id);
		return Result.ok(list);
	}

}

